namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System.Xml.Serialization;
    using SimpleWebServices;
    using Xunit;

    public sealed class LabelFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Label>()
                .DerivesFrom<InnerElements>()
                .IsConcreteClass()
                .IsUnsealed()
                .HasDefaultConstructor()
                .Implements<IAttributeAccesskey>()
                .Implements<IXmlSerializable>()
                .XmlRoot("label")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Label expected = new Label(new TextInput("control"), "title", "text")
            {
                Accesskey = 'a',
                Class = "class",
                Language = "fr"
            };

            string xml = "<label id='label-control' class='class' lang='fr' title='title' accesskey='a' for='control'>text</label>";
            Label actual = xml.Deserialize<Label>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void deserializeEmpty()
        {
            Assert.NotNull("<label />".Deserialize<Label>());
        }

        [Fact]
        public void serialize()
        {
            Label obj = new Label(new TextInput("control"), "title")
            {
                Accesskey = 'a',
                Class = "class",
                Language = "fr"
            };

            string xpath = "1=count(/label[@accesskey='a'][@for='control'][@id='label-control'][@class='class'][@lang='fr'][@title='title'])";

            Assert.True((bool)obj.Serialize().CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new Label());
        }

        [Fact]
        public void ctor_IControl_string()
        {
            IControl control = new TextInput("example");

            Label obj = new Label(control, "title");

            Assert.Equal<string>("label-" + control.Id, obj.Id);
            Assert.Equal<string>(control.Id, obj.For);
            Assert.Equal<string>("title", obj.Title);
            Assert.Equal<string>("title", (obj.Elements[0] as TextString).Value);
        }

        [Fact]
        public void ctor_IControl_string_string()
        {
            IControl control = new TextInput("example");

            Label obj = new Label(control, "title", "text");

            Assert.Equal<string>("label-" + control.Id, obj.Id);
            Assert.Equal<string>(control.Id, obj.For);
            Assert.Equal<string>("title", obj.Title);
            Assert.Equal<string>("text", (obj.Elements[0] as TextString).Value);
        }

        [Fact]
        public void prop_Accesskey()
        {
            Assert.True(new PropertyExpectations(typeof(Label).GetProperty("Accesskey"))
                .IsAutoProperty<char?>()
                .Result);
        }

        [Fact]
        public void prop_For()
        {
            Assert.True(new PropertyExpectations(typeof(Label).GetProperty("For"))
                .IsAutoProperty<Name>()
                .Result);
        }

        [Fact]
        public void prop_Title()
        {
            Assert.True(new PropertyExpectations(typeof(Label).GetProperty("Title"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void op_Deserialize_string_string_string()
        {
            Label expected = new Label(new TextInput("control"), "title", "text")
            {
                Accesskey = 'a',
                Class = "class",
                Language = "fr"
            };

            string xml = "<label id='label-control' class='class' lang='fr' title='title' accesskey='a' for='control'>text</label>";
            Label actual = new Label().Deserialize("label", null, xml) as Label;

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }
    }
}